package com.sjf.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sjf.common.BaseContext;
import com.sjf.reggie.entity.ShoppingCart;
import com.sjf.reggie.mapper.ShoppingCartMapper;
import com.sjf.reggie.service.ShoppingCartService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author <huangtang>
 * @since 2022/4/27 13:21
 **/
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Override
    public ShoppingCart add ( ShoppingCart shoppingCart ) {
        //设置userId
        shoppingCart.setUserId (BaseContext.getCurrentId ());
        //查询当前菜品是否在购物车中，如果在对数量进行操作即可
        ShoppingCart cart = null;
        if(shoppingCart.getDishId ()!= null){
            //查询的是菜品
            cart = this.getOne ( new QueryWrapper<ShoppingCart> ().lambda ().eq (ShoppingCart::getUserId, shoppingCart.getUserId () )
                                                      .eq (ShoppingCart::getDishId, shoppingCart.getDishId () ));

        }else{
            //查询的是套餐
            cart = this.getOne ( new QueryWrapper<ShoppingCart> ().lambda ().eq (ShoppingCart::getUserId, shoppingCart.getUserId () )
                                                      .eq (ShoppingCart::getSetmealId, shoppingCart.getSetmealId () ));
        }
        if ( cart != null ) {
            //如果存在，则对数量进行操作
            cart.setNumber (cart.getNumber ()+1);
            updateById ( cart );
        }else{
            //添加到购物车
            shoppingCart.setNumber (1);
            shoppingCart.setCreateTime (LocalDateTime.now ());
            save ( shoppingCart );
            cart = shoppingCart;
        }
        return cart;
    }

    @Override
    public List<ShoppingCart> getList () {
        Long userId = BaseContext.getCurrentId ();
        QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<> ();
        wrapper.lambda ().eq (ShoppingCart::getUserId, userId )
                .orderByAsc (ShoppingCart::getCreateTime);
        return list(wrapper);
    }

    @Override
    public ShoppingCart sub ( ShoppingCart shoppingCart ) {
        //设置userId
        shoppingCart.setUserId (BaseContext.getCurrentId ());
        //查看是菜品还是套餐
        ShoppingCart cart = null;
        if(shoppingCart.getDishId () != null){
            //查询的是菜品
             cart = this.getOne ( new QueryWrapper<ShoppingCart> ().lambda ().eq (ShoppingCart::getUserId, shoppingCart.getUserId () )
                                                      .eq (ShoppingCart::getDishId, shoppingCart.getDishId ()));
            //修改菜品数量
            if ( cart != null ) {
                cart.setNumber (cart.getNumber ()-1);
            }

        }else {
            //查询的是套餐
             cart = this.getOne ( new QueryWrapper<ShoppingCart> ().lambda ().eq (ShoppingCart::getUserId, shoppingCart.getUserId () )
                                                      .eq (ShoppingCart::getSetmealId, shoppingCart.getSetmealId ()));
            //修改套餐数量
            if ( cart != null ) {
                cart.setNumber (cart.getNumber ()-1);
            }
        }
        if(cart.getNumber () == 0){
            //如果数量为0，则删除
            removeById ( cart.getId () );
        }
        updateById ( cart );
        return cart;
    }

    @Override
    public void clean () {
        //清空当前用户购物车
        remove ( new QueryWrapper<ShoppingCart> ().lambda ().eq (ShoppingCart::getUserId, BaseContext.getCurrentId ()));
    }
}
